package com.neuedu.servlet.business;


import cn.hutool.core.util.StrUtil;
import com.google.gson.Gson;
import com.neuedu.pojo.Business;
import com.neuedu.service.BusinessService;
import com.neuedu.service.Imp.BusinessServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/busLogin")
public class BusinessLoginServlet extends HttpServlet {
    private BusinessService businessService = new BusinessServiceImp();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 请求参数的编码格式
        req.setCharacterEncoding("utf-8");

        String account = req.getParameter("account");
        String password = req.getParameter("password");

        /**
         * 根据不同的情况，返回不同的标记
         * 0：表示账号或密码为空
         * 1：表示账号和密码都存在，且能对应
         * 2：表示账号或密码不对应
         */



        if(StrUtil.isBlank(account) || StrUtil.isBlank(password)){
            resp.getWriter().write(0+"");
        }else{
            Business business = businessService.selectPart(account,password);
            if(business == null){
                resp.getWriter().write(2 + "");
            }else {
                resp.setContentType("application/json;charset=utf-8");
                Gson gson = new Gson();
                String json = gson.toJson(business);
                resp.getWriter().write(json);
            }
        }
    }
}
